var statusSel;
$(document).ready(function() {
	$('select[name="fieldId"]').change(function(){
		if ($(this).val() != "0") {
			$.blockUI({ message: ajaxAnimation }); 
			$.ajax({
			  url: 'searchCatByFieldId.html',
			  data : {'fieldId' : $(this).val()},
			  dataType : 'json',
			  success: function(data) {
				  	var catHtml = "";
					$.each(data.catList, function() {
						catHtml += '<option value="' + this.value + '">' + this.name + '</option>';
					});	
					$('select[name="cateId"]').html($('select[name="cateId"] :first').clone().wrap('<div>').parent().html() + catHtml);										
			  }
			});
		}else{
			$('select[name="cateId"]').html($('select[name="cateId"] :first').clone().wrap('<div>').parent().html());			
		}
	});

	$('select[name="groupId"]').change(function(){
		if ($(this).val() != "0") {
			$.blockUI({ message: ajaxAnimation }); 
			$.ajax({
			  url: 'searchUserByGroup.html',
			  data : {'groupId' : $(this).val()},
			  dataType : 'json',
			  success: function(data) {
				  	var groupHtml = "";
					$.each(data.listUser, function() {
						groupHtml += '<option value="' + this.value + '">' + this.name + '</option>';
					});			 
					$('select[name="userId"]').html($('select[name="userId"] :first').clone().wrap('<div>').parent().html() + groupHtml);										
			  }
			});
		}
	});

	new Calendar({
	    inputField: "toDate",
	    dateFormat: "%d/%m/%Y",
	    trigger: "toDateCal",
	    bottomBar: false,
	    onSelect: function() { this.hide(); }
	});

	new Calendar({
		inputField: "fromDate",
		dateFormat: "%d/%m/%Y",
		trigger: "fromDateCal",
		bottomBar: false,
		onSelect: function() 
		{ 
			$.ajax({
				  url: 'get-returned-date.html',
				  data : {'cateId' : $('select[name="cateId"]').val(), 'fromdate' : $('#fromDate').val()},
				  dataType : 'json',
				  success: function(data) {
					  $('#toDate').val(data.returnedDate);
				  }
				});
			this.hide(); 
		},
	});
	
	/*var cal = Calendar.setup({
        onSelect: function(cal) { cal.hide();},
        onChange: function(){
        	alert("Hello");
        }
    });
    cal.manageFields("fromDate","fromDate","%d/%m/%Y");
    cal.manageFields("toDate","toDate","%d/%m/%Y");*/
	
	$( '.cb-element' ).live('change', function(){
		$( '.cb-element' ).length == $( '.cb-element:checked' ).length ? 
				$( '.checkAll' ).attr( 'checked', 'checked' ): $( '.checkAll' ).attr( 'checked', '' );
		$( this ).next().val($( this ).is( ':checked' ));
	});
	$( '.checkAll' ).live('change', function(){
		$( '.cb-element' ).attr('checked', $( this ).is( ':checked' ) ? 'checked' : '');
		var chkAll = $( this );
		$( '.cb-element' ).each(function(){
			$( this ).next().val(chkAll.is( ':checked' ));
		});
	});
	
	$('#save').click(function(){
		/*$.blockUI({ message: ajaxAnimation });*/
		$.blockUI({ message: '<img src="${ctx}/images/busy.gif" />...' });
		$.ajax({
		  type: 'POST',
		  url: $('form').attr('action') + '?struts.enableJSONValidation=true&struts.validateOnly=true',
		  data: $('form').serialize(),
		  success: function(data){
			 var errorsObject = $.StrutsUtils.getValidationErrors(data);
			 if(errorsObject.fieldErrors) {
		       	//StrutsUtils.showValidationErrors(form, errorsObject);
				 $.StrutsUtils.showValidationErrors($('form'), errorsObject);
		     } else if(errorsObject.errors) {
		       	//StrutsUtils.showValidationErrors(form, errorsObject);
				 $.StrutsUtils.showValidationErrors($('form'), errorsObject);
		     } else {
		       //good to go, regular submit
		       $('form').submit();
		     }
		  },
		  dataType: "text/html"
		});
	});
});
